<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Model\Fruit;
use App\Http\Model\Need;
use App\Http\Model\Assess;
use App\Http\Model\Hatch;
use App\Http\Model\Bank;
use App\Http\Model\Link;
use App\Http\Model\Laboratory;
use App\Http\Model\Expert;
use App\Http\Model\Video;
use App\Http\Model\User;
use App\Http\Model\Banner;

require_once('resources/org/code/Code.class.php');

class IndexController extends Controller
{
    public function login(){

        if($input=Input::all()){

            $code=new \Code;
            $_code=$code->get();
            if(strtoupper($input['code'])!=$_code){
                return back()->with('msg','验证码错误！');  //将错误信心返回提交页面，错误信息存放在session中
            }

            $users=User::where('username',$input['username'])->first();

            if(!$users || $users->username!=$input['username'] || Crypt::decrypt($users->password)!=$input['password']){
                return back()->with('msg','用户名或密码错误！');  //将错误信心返回提交页面，错误信息存放在session中
            }

            session(['users'=>$users]);
            return redirect('/');


        }else{

            return view('admin.login');

        }

    }

    public function index(){

        $frudata=Fruit::where('fru_state','2')->orderBy('fru_time','desc')->take(4)->get();
        $needata=Need::where('nee_state','2')->orderBy('nee_time','desc')->take(5)->get();
        $assdata=Assess::where('ass_state','2')->orderBy('ass_time','desc')->take(5)->get();
        $hatdata=Hatch::where('hat_state','2')->orderBy('hat_time','desc')->take(5)->get();
        $bandata=Bank::where('ban_state','2')->orderBy('ban_time','desc')->take(5)->get();

        $banner=Banner::orderBy('banner_time','desc')->take(5)->get();

        $frudata2=Fruit::where('fru_state','2')->orderBy('fru_time','desc')->take(5)->get();

        return view('home.main',compact('frudata','frudata2','needata','assdata','hatdata','bandata','banner'));

    }


    public function fruit(){

        $domain=isset($_GET['domain'])?$_GET['domain']:'不限';
        $rate=isset($_GET['rate'])?$_GET['rate']:'不限';

        if($domain !='不限' && $rate !='不限' ){
            $frudata=Fruit::where('fru_state','2')->where('fru_class',$domain)->where('fru_rate',$rate)->orderBy('fru_time','desc')->paginate(15);
        }elseif($domain !='不限'){
            $frudata=Fruit::where('fru_state','2')->where('fru_class',$domain)->orderBy('fru_time','desc')->paginate(15);
        }elseif($rate !='不限'){
            $frudata=Fruit::where('fru_state','2')->where('fru_rate',$rate)->orderBy('fru_time','desc')->paginate(15);
        }else{
            $frudata=Fruit::where('fru_state','2')->orderBy('fru_time','desc')->paginate(15);
        }        

        $fruview=Fruit::where('fru_state','2')->orderBy('fru_time','desc')->take(5)->get();
        $frutime=Fruit::where('fru_state','2')->orderBy('fru_time','desc')->take(5)->get();
        return view('home.fruit',compact('frudata','fruview','frutime'));

    }

    public function need(){

        $domain=isset($_GET['domain'])?$_GET['domain']:'不限';
        $rate=isset($_GET['rate'])?$_GET['rate']:'不限';
        $method=isset($_GET['method'])?$_GET['method']:'不限';

        if($domain !='不限' && $rate !='不限' && $method !='不限'){
            $needata=Need::where('nee_state','2')->where('nee_class',$domain)->where('nee_rate',$rate)->where('nee_method',$method)->orderBy('nee_time','desc')->paginate(15);
        }elseif($domain !='不限' && $rate !='不限'){
            $needata=Need::where('nee_state','2')->where('nee_class',$domain)->where('nee_rate',$rate)->orderBy('nee_time','desc')->paginate(15);
        }elseif($domain !='不限' && $method !='不限'){
            $needata=Need::where('nee_state','2')->where('nee_class',$domain)->where('nee_method',$method)->orderBy('nee_time','desc')->paginate(15);
        }elseif($rate !='不限' && $method !='不限'){
            $needata=Need::where('nee_state','2')->where('nee_rate',$rate)->where('nee_method',$method)->orderBy('nee_time','desc')->paginate(15);
        }elseif($domain !='不限'){
            $needata=Need::where('nee_state','2')->where('nee_class',$domain)->orderBy('nee_time','desc')->paginate(15);
        }elseif($rate !='不限'){
            $needata=Need::where('nee_state','2')->where('nee_rate',$rate)->orderBy('nee_time','desc')->paginate(15);
        }elseif($method !='不限'){
            $needata=Need::where('nee_state','2')->where('nee_method',$method)->orderBy('nee_time','desc')->paginate(15);
        }else{
            $needata=Need::where('nee_state','2')->orderBy('nee_time','desc')->paginate(15);
        }

        $neeview=Need::where('nee_state','2')->orderBy('nee_time','desc')->take(5)->get();
        $neetime=Need::where('nee_state','2')->orderBy('nee_time','desc')->take(5)->get();
        return view('home.need',compact('needata','neeview','neetime'));

    }

    public function assess(){

        $assview=Assess::where('ass_state','2')->orderBy('ass_time','desc')->take(5)->get();
        $asstime=Assess::where('ass_state','2')->orderBy('ass_time','desc')->take(5)->get();
        return view('home.assess',compact('assview','asstime'));

    }

    public function hatch(){

        $domain=isset($_GET['domain'])?$_GET['domain']:'不限';

        if($domain !='不限'){
           $hatdata=Hatch::where('hat_state','2')->where('hat_domain',$domain)->orderBy('hat_time','desc')->paginate(15);
        }else{
            $hatdata=Hatch::where('hat_state','2')->orderBy('hat_time','desc')->paginate(15);
        }
        
        $hatview=Hatch::where('hat_state','2')->orderBy('hat_time','desc')->take(5)->get();
        $hattime=Hatch::where('hat_state','2')->orderBy('hat_time','desc')->take(5)->get();
        return view('home.hatch',compact('hatdata','hatview','hattime'));

    }

    public function bank(){

        $domain=isset($_GET['domain'])?$_GET['domain']:'不限';
        $type=isset($_GET['type'])?$_GET['type']:'不限';
        $method=isset($_GET['method'])?$_GET['method']:'不限';

        if($domain !='不限' && $type !='不限' && $method !='不限'){
            $bandata=Bank::where('ban_state','2')->where('ban_domain',$domain)->where('ban_type',$type)->where('ban_method',$method)->orderBy('ban_time','desc')->paginate(15);
        }elseif($domain !='不限' && $type !='不限'){
            $bandata=Bank::where('ban_state','2')->where('ban_domain',$domain)->where('ban_type',$type)->orderBy('ban_time','desc')->paginate(15);
        }elseif($domain !='不限' && $method !='不限'){
            $bandata=Bank::where('ban_state','2')->where('ban_domain',$domain)->where('ban_method',$method)->orderBy('ban_time','desc')->paginate(15);
        }elseif($type !='不限' && $method !='不限'){
            $bandata=Bank::where('ban_state','2')->where('ban_type',$type)->where('ban_method',$method)->orderBy('ban_time','desc')->paginate(15);
        }elseif($domain !='不限'){
            $bandata=Bank::where('ban_state','2')->where('ban_domain',$domain)->orderBy('ban_time','desc')->paginate(15);
        }elseif($type !='不限'){
            $bandata=Bank::where('ban_state','2')->where('ban_type',$type)->orderBy('ban_time','desc')->paginate(15);
        }elseif($method !='不限'){
            $bandata=Bank::where('ban_state','2')->where('ban_method',$method)->orderBy('ban_time','desc')->paginate(15);
        }else{
            $bandata=Bank::where('ban_state','2')->orderBy('ban_time','desc')->paginate(15);
        }

        $banview=Bank::where('ban_state','2')->orderBy('ban_time','desc')->take(5)->get();
        $bantime=Bank::where('ban_state','2')->orderBy('ban_time','desc')->take(5)->get();
        return view('home.bank',compact('bandata','banview','bantime'));

    }

    public function link(){

        $lindata1=Link::where('lin_class','政策导航')->orderBy('lin_time','desc')->get();
        $lindata2=Link::where('lin_class','资讯导航')->orderBy('lin_time','desc')->get();
        $lindata3=Link::where('lin_class','专利导航')->orderBy('lin_time','desc')->get();
        $lindata4=Link::where('lin_class','活动导航')->orderBy('lin_time','desc')->get();
        return view('home.link',compact('lindata1','lindata2','lindata3','lindata4'));

    }

    public function laboratory(){

        $labdata=Laboratory::orderBy('lab_time','desc')->get();
        return view('home.laboratory',compact('labdata'));

    }

    public function expert(){


        $type=isset($_GET['type'])?$_GET['type']:'不限';
        $domain=isset($_GET['domain'])?$_GET['domain']:'不限';
        $title=isset($_GET['title'])?$_GET['title']:'不限';

        if($domain !='不限' && $type !='不限' && $title !='不限'){
            $expdata=Expert::where('exp_domain',$domain)->where('exp_type',$type)->where('exp_title',$title)->orderBy('exp_time','desc')->paginate(15);
        }elseif($domain !='不限' && $type !='不限'){
            $expdata=Expert::where('exp_domain',$domain)->where('exp_type',$type)->orderBy('exp_time','desc')->paginate(15);
        }elseif($domain !='不限' && $title !='不限'){
            $expdata=Expert::where('exp_domain',$domain)->where('exp_title',$title)->orderBy('exp_time','desc')->paginate(15);
        }elseif($type !='不限' && $title !='不限'){
            $expdata=Expert::where('exp_type',$type)->where('exp_title',$title)->orderBy('exp_time','desc')->paginate(15);
        }elseif($domain !='不限'){
            $expdata=Expert::where('exp_domain',$domain)->orderBy('exp_time','desc')->paginate(15);
        }elseif($type !='不限'){
            $expdata=Expert::where('exp_type',$type)->orderBy('exp_time','desc')->paginate(15);
        }elseif($title !='不限'){
            $expdata=Expert::where('exp_title',$title)->orderBy('exp_time','desc')->paginate(15);
        }else{
            $expdata=Expert::orderBy('exp_time','desc')->paginate(15);
        }

        return view('home.expert',compact('expdata'));

    }

    public function videos(){

        $viddata=Video::orderBy('vid_time','desc')->get();
        return view('home.video',compact('viddata'));

    }

    public function quit(){

        session(['users'=>null]);
        return redirect('/');
        
    }

    public function search(){

        $input=Input::all();

        if($input['search']){

            $search=trim($input['search']);
            $select=$input['select'];

            if($select=='全站'){
                $fruit = DB::select("select * from Fruit where fru_author like '%{$search}%' or fru_name like '%{$search}%' order by fru_time desc");

                $need = DB::select("select * from Need where nee_author like '%{$search}%' or nee_name like '%{$search}%' order by nee_time desc");

                $assess = DB::select("select * from Assess where ass_author like '%{$search}%' or ass_name like '%{$search}%' order by ass_time desc");

                $hatch = DB::select("select * from Hatch where hat_author like '%{$search}%' or hat_name like '%{$search}%' order by hat_time desc");

                $bank = DB::select("select * from Bank where ban_author like '%{$search}%' or ban_name like '%{$search}%' order by ban_time desc");

                $expert = DB::select("select * from Expert where exp_name like '%{$search}%' or exp_title like '%{$search}%' order by exp_time desc");

                $data=array_merge($fruit,$need,$assess,$hatch,$bank,$expert);
                
                return view('search',compact('data','search','select'));
            }

            if($select=='科技成果'){
                $fruit = DB::select("select * from Fruit where fru_author like '%{$search}%' or fru_name like '%{$search}%' order by fru_time desc");

                $data=array_merge($fruit);
                
                return view('search',compact('data','search','select'));
            }

            if($select=='科技需求'){
                $need = DB::select("select * from Need where nee_author like '%{$search}%' or nee_name like '%{$search}%' order by nee_time desc");

                $data=array_merge($need);
                
                return view('search',compact('data','search','select'));
            }

            if($select=='科技孵化'){
                $assess = DB::select("select * from Assess where ass_author like '%{$search}%' or ass_name like '%{$search}%' order by ass_time desc");

                $hatch = DB::select("select * from Hatch where hat_author like '%{$search}%' or hat_name like '%{$search}%' order by hat_time desc");

                $bank = DB::select("select * from Bank where ban_author like '%{$search}%' or ban_name like '%{$search}%' order by ban_time desc");

                $data=array_merge($assess,$hatch,$bank);
                
                return view('search',compact('data','search','select'));
            }

            if($select=='科技专家库'){
                $expert = DB::select("select * from Expert where exp_name like '%{$search}%' or exp_title like '%{$search}%' order by exp_time desc");

                $data=array_merge($expert);
                
                return view('search',compact('data','search','select'));
            }

        }else{
            return redirect('/');
        }        

    }
}
